package sd.czw.com.mmgapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.customviews.FlowLayoutRadioGroup;
import sd.czw.com.mmgapp.model.goodsbean.GoodsDetailsModel;
import sd.czw.com.mmgapp.model.goodsbean.GoodsdetailleftModel;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/20.
 */
public class ShopSpecAdapter extends RecyclerView.Adapter<ShopSpecAdapter.SpecViewHolder>{

    private Context context;
    private List<GoodsdetailleftModel.ErrmsgBean.GoodsBean.SpecificationBean> datas;
    private List<GoodsdetailleftModel.ErrmsgBean.GoodsBean.SpecificationBean.ValuesBean> gdatas;
    private CzwRvfindItemClick czwRvFindItemClick;
    private Map<Integer,Integer> map = new TreeMap<>();

    public void setCzwRvFindItemClick(CzwRvfindItemClick czwRvFindItemClick) {
        this.czwRvFindItemClick = czwRvFindItemClick;
    }

    public interface CzwRvfindItemClick{
        void onRvFindItemClick(Map<Integer,Integer> map);
    }

    public Map<Integer, Integer> getMap() {
        return map;
    }

    public ShopSpecAdapter(Context context, List<GoodsdetailleftModel.ErrmsgBean.GoodsBean.SpecificationBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public SpecViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SpecViewHolder(LayoutInflater.from(context).inflate(R.layout.place_rv,parent,false));
    }

    @Override
    public void onBindViewHolder(SpecViewHolder holder, final int position) {
        holder.tvtitle.setText(datas.get(position).getName());
        RadioGroup.LayoutParams paras = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);
        paras.setMargins(0,10,20,10);
        gdatas = datas.get(position).getValues();
        final RadioButton[] rblist = new RadioButton[gdatas.size()];
        for (int i =0;i<datas.get(position).getValues().size();i++){
            RadioButton rb = new RadioButton(context);
            rb.setGravity(Gravity.CENTER);
            rb.setTextColor(Color.WHITE);
            rb.setBackgroundResource(R.drawable.sel_radio_button);
            rb.setButtonDrawable(R.drawable.button_null);
            rb.setLayoutParams(paras);
            rblist[i] = rb;
            rblist[i].setText(gdatas.get(i).getLabel());
            rblist[i].setTag(i);
            holder.rbutton.addView(rblist[i]);
        }

        //绑定标签的Click事件
        for (int j = 0; j < rblist.length; j++) {
            rblist[j].setTag(rblist);
            final int finalJ1 = j;
            rblist[j].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    map.put(position, finalJ1);
                    //czwRvFindItemClick.onRvFindItemClick(map);
                    // czwRvFindItemClick.onRvFindItemClick(rblist[finalJ1],position,finalJ1);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class SpecViewHolder extends RecyclerView.ViewHolder{

        TextView tvtitle;
        FlowLayoutRadioGroup rbutton;
        public SpecViewHolder(View itemView) {
            super(itemView);
            tvtitle = (TextView) itemView.findViewById(R.id.rv_title);
            rbutton = (FlowLayoutRadioGroup) itemView.findViewById(R.id.rg);
        }
    }
}
